<template id="forgot_password">
	<div class="forgot_password">

		<!--登陆标题-->
		<div class="login_title">
			<p>绑定用户</p>
		</div>
		<!--登陆标题结束-->

		<!--请输入手机号/密码-->
		<div class="login_entry">

			<!--请输入手机号-->
			<div class="login_tel">
				<div class="login_tel_left">
					<img src="../../assets/images/login/phone.png" />
					<input type="number" id="messageInput" placeholder="请输入手机号" value="" v-model="phone" @change="phhotoChange($event)" />
				</div>
				<div class="login_tel_right" @click="model()">
					<img src="../../assets/images/login/clear.png">
				</div>
			</div>

			<!--请输入短信验证-->
			<div class="login_vaildate">
				<div class="login_vaildate_left">
					<img src="../../assets/images/login/check.png" />
					<input type="number" name="" id="" placeholder="请输入短信验证" v-model="code" />
				</div>
				<div class="login_vaildate_right" @click="Code()">
					<button :disabled="!sendAuthCode" :style="{background: sendAuthCode ? '#2d8f90' : '#ccc'}"> {{sendAuthCode ? '获取验证码' : '重新获取' + auth_time + '秒' }}</button>
				</div>

			</div>

		</div>
		<!--请输入手机号/密码结束-->

		<!--登陆按钮-->
		<div class="login_press" @click.stop="Landed()">
			<router-link to=""><button>登陆</button></router-link>
		</div>
		<!--登陆按钮结束-->

		<!--未注册手机验证码后自动注册-->

		<!--微信登陆-->
		

		<!--微信登陆结束-->

		<!--用户协议及隐私政策-->
		<div class="edit_policy">
			<img src="../../assets/images/main/pitch_on.png" />
			<span>用户协议及隐私政策</span>
		</div>
		<!--用户协议及隐私政策结束-->

	</div>
</template>

<script>
	export default {
		name: 'forgot_password',
		components: {},
		data() {
			return {
				sendAuthCode: true,
				/*布尔值，通过v-show控制显示‘获取按钮’还是‘倒计时’ */
				auth_time: 0,
				/*倒计时 计数器*/
				phone: "", //手机号
				code: "", //验证码
				coachid: "",
			    apiurl: "http://yogahome.top/yujia/user/wexinlogin",
			    openid: "",
			}
		},

		created() {
			var param = location.href.split("?");
			var paramarr = param[1].split("&");
			var code = paramarr[0].split("=")[1];
			var state = paramarr[1].split("=")[1].split("#")[0];
			console.log(code + ":" + state);

			let obj = {
				//请求的参数
				code,
				state,
			};
			let _this = this;
			let qs = require('qs');
			this.$axios.post(_this.apiurl,
					qs.stringify(obj), {
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						}
						//重写headers 请求头  qs.stringify(json对象) 把对象转化为字符串
					}
				).then(response => {
					console.log(response);
					if(response.data.code == 1) {
						//首页登陆成功
						alert("登陆成功")
						_this.$router.push('/')
					} else if(response.data.code == 0) {

						alert("请绑定用户!");
						_this.openid = response.data.data;
					} else if(response.data.code == -1) {
						//登陆页面
						alert("登陆失败,请重新登录")
						_this.$router.push('/forgot_password')
					} else if(response.data.code == -2) {
						//服务器状态码错误state
						alert("登陆状态异常,请重新登陆")
						_this.$router.push('/forgot_password')
					}
				})
				.catch(function(error) {
					console.log(error);
				});
		},
		methods: {
			model() {
				this.phone = "";

			},
			phhotoChange(e) {
				this.phone = e.target.value;
			},
			Code() {
				
				let re = /^1\d{10}$/;
				let _this = this;
				if(this.phone) {
					if(re.test(_this.phone)) {
						var phone = _this.phone;
						let obj = {
							phone
						};
						let qs = require('qs');
						_this.$axios.post("http://yogahome.top/yujia/user/captcha", qs.stringify(obj), {
							headers: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
						}).then(function(response) {
							_this.sendAuthCode = false;
							_this.auth_time = 60;
							var auth_timetimer = setInterval(() => {
								_this.auth_time--;
								if(_this.auth_time <= 0) {
									_this.sendAuthCode = true;
									clearInterval(auth_timetimer);
								}
							}, 1000);

						}).catch(function(error) {
							console.log(error);
						});

					} else {
						_this.sendAuthCode = true;
						alert('手机号格式不正确,请重新输入')
					}

				} else {
					_this.sendAuthCode = true;
					alert('请输入手机号码')
				}

			},
			
			test(){
				console.log(this.openid)
			},
			Landed() {
				let _this = this
				if(this.phone) {
					var phone = this.phone; //手机号
					var code = this.code; //验证码
					var openid = _this.openid;
					let obj = {
						phone,
						code,
						openid,

					};

					if(this.phone == "") {

					} else {

					}

					let qs = require('qs');
					this.$axios.post("http://yogahome.top/yujia/user/binduser", qs.stringify(obj), {
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},

					}).then(function(response) {
						//	
						let code = response.data.code;
						if(code == -1) {
							alert(response.data.message);
						} else if(code == -2) {
              alert(response.data.message);
							_this.$router.push('/forgot_password');
						} else if(code == -3) {
              alert(response.data.message);
						} else if(code == 0) {
              alert(response.data.message);
						} else if(code == 1) {
              alert(response.data.message);
							_this.$router.push('/');
						} else if(code == 2) {
              alert(response.data.message);
						}
					}).catch(function(error) {
						console.log(error);
					})
				} else {
					alert("请输入手机号码")
				}

			},
		},
	}
</script>

<style>
	@import "../../assets/css/forgot_password.css";
</style>
